<?php
/**
 * Created by PhpStorm.
 * User: hd-1
 * Date: 2018/8/18
 * Time: 13:07
 */
namespace app\admin\controller;


use app\common\lib\Tool;
use think\Db;
use think\Request;

class Article extends Base
{
    public function index()
    {
        $Arr = input();
        if(!empty($Arr['title'])){
            $map[] =  ['title', 'like', "%".$Arr['title']."%"];
        }else{
            $map = [];
        }
        $data = Db::table('article')
                        ->alias('a')
                        ->field('a.*,b.name')
                        ->join('category b','a.cate_id = b.id')
                        ->where($map)
                        ->order('a.id asc')
                        ->paginate(5,false,['query' => request()->param()]);
        $page = $data->render();
        return view('demo/order-list',['data'=>$data,'page'=>$page,'map'=>$Arr]);
    }

    public function add( Request $request){
        if($request->isPost()){
            $Arr = input();
            $data = [
                'title'=>$Arr['username'],
                'cate_id'=>$Arr['articleType'],
                'content'=>$_POST['myEditor'],
                'picture'=>$Arr['imgArticle'],
                'keywords'=>$Arr['keywords'],
                'description'=>$Arr['description'],
                'add_time'=>time(),
                'status'=>1,
            ];
            $Ret = Db::table('article')->insert($data);
            if($Ret){
                return Tool::json(1,[],'添加文章成功');
            }else{
                return Tool::json(0,[],'添加文章失败');
            }
        }else{
            $data = Db::table('category')->select();
            return view('demo/order-add',['TypeData'=>$data]);
        }
    }

    public function update( Request $request){
        $Arr = input();
        if($request->isPost()){
            $data = [
                'title'=>$Arr['username'],
                'cate_id'=>$Arr['articleType'],
                'content'=>$Arr['myEditor'],
                'picture'=>$Arr['imgArticle'],
                'keywords'=>$Arr['keywords'],
                'description'=>$Arr['description'],
                'add_time'=>strtotime($Arr['addTime']),
                'status'=>1,
            ];
            $map['id'] = $Arr['id'];
            $Ret = Db::table('article')->where($map)->update($data);
            if($Ret){
                return Tool::json(1,[],'修改文章成功');
            }else{
                return Tool::json(0,[],'修改文章失败');
            }
        }else{
            $data = Db::table('category')->select();
            $articleArr = Db::table('article')->where(['id'=>$Arr['id']])->find();
            return view('demo/order_update',['TypeData'=>$data,'article'=>$articleArr]);
        }
    }

    public function deleteArticle(){
        $id = input('id');
        $Ret = Db::table('article')->where('id',$id)->delete();
        if($Ret){
            return Tool::json(1,[],'删除文章成功');
        }else{
            return Tool::json(0,[],'删除文章失败');
        }
    }
}